package com.spring.security.web.service.impl;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.springframework.stereotype.Service;

import com.spring.security.core.context.JedisCont;
import com.spring.security.core.util.ApplicationUtils;
import com.spring.security.web.model.Ihuyi;
import com.spring.security.web.model.Status;
import com.spring.security.web.service.IhuyiService;

/**
 * 短信发送接口
 * @author wojunyu
 *
 */
@Service
public class IhuyiServiceImpl implements IhuyiService{
	private static final String Url = JedisCont.getProParm("IHUYI_URL");
	private static final String Account = JedisCont.getProParm("IHUYI_ACCOUNT");
	private static final String Password = JedisCont.getProParm("IHUYI_PASSWORD");
	@Override
	public Status sendSms(Ihuyi info){
		Status ret = new Status();
		if(!ApplicationUtils.isNotNull(Url)){
			ret.setStatus("901");
			ret.setMsg("短信接口调用地址不能为空");
			return ret;
		}
		if(!ApplicationUtils.isNotNull(Account)){
			ret.setStatus("902");
			ret.setMsg("账号名不能为空");
			return ret;
		}
		if(!ApplicationUtils.isNotNull(Password)){
			ret.setStatus("903");
			ret.setMsg("账号密码不能为空");
			return ret;
		}
		if(!ApplicationUtils.isNotNull(info.getMobile())){
			ret.setStatus("904");
			ret.setMsg("手机号不能为空");
			return ret;
		}
		if(!ApplicationUtils.isNotNull(info.getContent())){
			ret.setStatus("905");
			ret.setMsg("内容不能为空");
			return ret;
		}
		
		try {
			List<NameValuePair> formparams = new ArrayList<NameValuePair>();
	        formparams.add(new BasicNameValuePair("account", Account));
	        formparams.add(new BasicNameValuePair("password", Password));
	        formparams.add(new BasicNameValuePair("mobile", info.getMobile()));
	        formparams.add(new BasicNameValuePair("content", info.getContent()));
	        HttpEntity reqEntity;
			
				reqEntity = new UrlEncodedFormEntity(formparams, "utf-8");
			
	 
	        RequestConfig requestConfig = RequestConfig.custom()
	                .setSocketTimeout(5000)
	                .setConnectTimeout(5000)
	                .setConnectionRequestTimeout(5000)
	                .build();
	 
	        HttpClient client = new DefaultHttpClient();
	        HttpPost post = new HttpPost(Url);
	        post.setEntity(reqEntity);
	        post.setConfig(requestConfig);
	        HttpResponse response = client.execute(post);
	 
	        if (response.getStatusLine().getStatusCode() == 200) {
	            HttpEntity resEntity = response.getEntity();
//	            String message = EntityUtils.toString(resEntity, "utf-8");
//	            System.out.println(message);
	            ret.setStatus("0");
	            ret.setMsg("发送成功");
//	            ret.setMsg(message);
//	            PraseRetCode(message);
	        } else {
	            ret.setStatus("906");
	            ret.setMsg("请求失败");
	        }
        
		} catch (Exception e) {
			e.printStackTrace();
			ret.setStatus("907");
            ret.setMsg("请求发生异常");
		}
		return ret;
		
	}
	
	public class IhuyiRetCode{
		private String code;
		private String msg;
		private String smsid;
		public String getCode() {
			return code;
		}
		public void setCode(String code) {
			this.code = code;
		}
		public String getMsg() {
			return msg;
		}
		public void setMsg(String msg) {
			this.msg = msg;
		}
		public String getSmsid() {
			return smsid;
		}
		public void setSmsid(String smsid) {
			this.smsid = smsid;
		}
	}
	
	public IhuyiRetCode PraseRetCode(String inputXML){
		System.out.println(inputXML);
		IhuyiRetCode ret = new IhuyiRetCode();
		Document doc;
		try {
			doc = DocumentHelper.parseText(inputXML);
			 Element rootElt = doc.getRootElement(); // 获取根节点
	         System.out.println("根节点：" + rootElt.getName()); // 拿到根节点的名称
	         Iterator iter = rootElt.elementIterator("SubmitResult"); // 获取根节点下的子节点datas
	         while (iter.hasNext()) {
	        	 Element recordEle = (Element) iter.next();
	        	 System.out.println("子节点：" + recordEle.getName());
	        	 System.out.println(recordEle.elementTextTrim(recordEle.getName()));
	         }
		} catch (Exception e) {
			e.printStackTrace();
		}
        
		return ret;
	}
	
//	public static void main(String[] args) {
//		Ihuyi info = new Ihuyi();
//		IhuyiServiceImpl l = new IhuyiServiceImpl();
//		info.setMobile("15058253569");
//		info.setContent("您的验证码是：【234324324】。请不要把验证码泄露给其他人。");
//		Status s = l.sendSms(info);
//		System.out.println(s.getStatus());
//	}
	
}
